<?php

namespace App\Http\Controllers\Admin;

use App\Models\Role;
use App\Models\User;
use Illuminate\Http\Request;
use Hash;


class UserController extends BaseController
{
    //用户列表显示
    public function index()
    {
        //分页获取数据
        $data = User::all();
        //返回视图 数据
        return view('admin.user.index',compact('data'));
    }

    //用户添加
    public function create()
    {
        return view('admin.user.create');
    }

    //用户添加处理
    public function store(Request $request)
    {
        try {
            $this->validate($request,[
                'username'   =>  'required|unique:users,username',
                'truename'   =>  'required',
                'password'   =>  'required'
            ]);
        }catch (\Exception $e) {
            return ['status' => 1000, 'msg' => '用户名已存在'];
        }

        $data = $request->except(['_token','password2']);
        $data['password'] = bcrypt($data['password']);

        if(User::create($data)){
            return ['status'=>0,'msg'=>'添加成功'];
        }

    }

    //全选删除
    public function delAll(Request $request)
    {
        //获取id数组
        $ids = $request->get('id');
        //执行删除
        User::destroy($ids);
        return ['status'=>0,'msg'=>'全选删除成功'];
    }

    //用户修改显示
    public function edit(User $user)
    {
        return view('admin.user.edit',compact('user'));
    }

    //用户修改处理
    public function update(Request $request, User $user)
    {
        $data = $request->only(['truename','password','phone','sex','email']);
        if(!empty($data['password'])){
            $data['password'] = bcrypt($data['password']);
        }else{
            unset($data['password']);
        }
        $user->update($data);
        return ['status'=>0,'msg'=>'修改成功'];



    }

    //个人修改显示
    public function myedit(Request $request)
    {
        $id = $request->get('id');
        $user = User::find($id);
        return view('admin.user.myedit',compact('user'));
    }

    //个人修改处理
    public function myupdate(Request $request, User $user)
    {
        //明文密码
        $spass = $request->get('spassword');
        //密文密码
        $mpass = $user->password;
        //验证
        $bool = Hash::check($spass,$mpass);
        if($bool){
            $data = $request->only(['truename','password','phone','sex','email']);
            if(!empty($data['password'])){
                $data['password'] = bcrypt($data['password']);
            }else{
                unset($data['password']);
            }
            $user->update($data);
            return ['status'=>0,'msg'=>'修改成功'];
        }else{
            return ['status'=>1000,'msg'=>'原密码不正确'];
        }

    }

    //用户删除
    public function destroy(User $user)
    {
        $user->delete();
        return ['status'=>0,'msg'=>'删除成功'];
    }

    //分配角色和处理
    public function role(Request $request,User $user){
        if($request->isMethod('post')){
            $post = $this->validate($request,[
                'role_id' => 'required'
            ]);
            $user->update($post);
            return ['status'=>0,'msg'=>'分配角色成功'];
        }
        //读取角色
        $roleAll =Role::all();
        return  view('admin.user.role',compact('user','roleAll'));
    }
}
